package com.slj.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
//import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;

public class JdbcConfig {
        @Value("driver")
        private String driver;
        @Value("url")
        private String url;
        @Value("name")
        private String username;
        @Value("password")
        private String password;

        @Bean
        public DataSource dataSource() {
            //创建数据源对象
                DruidDataSource dataSource = new DruidDataSource();
                dataSource.setDriverClassName(driver);
                dataSource.setUrl(url);
                dataSource.setUsername(username);
                dataSource.setPassword(password);
                return dataSource;
        }
        //创建事务管理器 负责管理事务的创建,提交回滚等操作
        @Bean
        public PlatformTransactionManager transactionManager(DataSource dataSource){
                DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
                transactionManager.setDataSource(dataSource);
                return transactionManager;
        }
}
